package com.example.microinvestmarket.service.impl;
import com.example.microinvestcommon.pojo.Result;
import com.example.microinvestcommon.util.ChangeListAndJsonUtil;
import com.example.microinvestcommon.vo.StockNumSymbolVo;
import com.example.microinvestcommon.vo.StockOneFiveMinDataVo;
import com.example.microinvestmarket.dao.CreateStockMapper;
import com.example.microinvestmarket.dao.StockDataGetMapper;
import com.example.microinvestmarket.service.StockDataGetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tianwailaiwu
 * 用于获取Stock数据
 */
@Service
public class StockDataGetServiceImpl implements StockDataGetService {

    private final StockDataGetMapper stockDataGetMapper;
    private ChangeListAndJsonUtil changeListToJsonUtil;
    private final CreateStockMapper createStockMapper;

    @Autowired
    public StockDataGetServiceImpl(StockDataGetMapper stockDataGetMapper,CreateStockMapper createStockMapper){
        this.stockDataGetMapper=stockDataGetMapper;
        this.createStockMapper=createStockMapper;
    }

    @Override
    public Result<List<Double>> getStockOneFiveMinData(String stockSymbol) {

        String stockJson = createStockMapper.getStockMessage(stockSymbol);

        List<List<Double>> stockDataList = null;
        try {
            stockDataList = changeListToJsonUtil.changeToList(stockJson);

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取信息失败");
        }

        List<Double> latestStockData=stockDataList.get(stockDataList.size()-1);

        return Result.success(latestStockData);


    }

    @Override
    public Result<List<String>> getAllStockName() {
        List<String> stockSymbols=null;
        try {
            stockSymbols=stockDataGetMapper.getAllStockSymbol();
        }catch (Exception e){
            return Result.error("获取股票名字服务出现异常");
        }

        return Result.success(stockSymbols);
    }

    @Override
    public Result<List<StockNumSymbolVo>> getAllStocksNumAndSymbol() {
        List<StockNumSymbolVo> stockNumSymbolVos=null;
        try {
            {
                stockNumSymbolVos=stockDataGetMapper.getAllStocksNumAndSymbol();
            }
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
        return Result.success(stockNumSymbolVos);
    }
}
